package com.cloudptt.api.product.func.call.group.fsm.floor;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.log.Log;

public class FloorNoPermissionState implements ISubState
{
	private FloorAgent agent = null;
//	private String name = "";
	private boolean isIdle = true;
	public FloorNoPermissionState(FloorAgent agent)
	{
		this.agent = agent;
		Log.d(FloorAgent.Log_Key, "currentState is FloorNoPermissionState");
		this.agent.opAudioMedia(CallAttr.OpMedia_Pause);
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Request == btnAction)
		{
//			if(isIdle)
//			{
				int result = agent.floorRequest();
				if(CauseCont.OperateCheck_Suc == result)
				{
					agent.goToState(CallConst.FloorState_PendingRequset,null);
				}
				return result;
//			}
//			else
//			{
//				return CauseCont.OperateErr_RightOccupy;
//			}
		}
		else if(CallConst.BtnAction_Release == btnAction)
		{
		}
		return CauseCont.OperateCheck_Suc;
	}

	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			Log.d(FloorAgent.Log_Key, "currentState is IdleState msg == null");
			return;
		}
		String title = msg.getTitle();
		Log.d(FloorAgent.Log_Key, "receiveRtcpMsg title = " + title);
		if(title.equals(RtcpMsg.Title_FLOOR_TAKEN))
		{
			Log.d(FloorAgent.Log_Key, "receiveRtcpMsg isIdle = " + isIdle);
//			if(isIdle)
			{
				String num = msg.getTakenNum();
				Log.d(FloorAgent.Log_Key, "receiveRtcpMsg num = " + num);
				this.agent.updateSpeakPermissionNum(num);
			}

			isIdle = false;
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_IDLE))
		{
			if(!isIdle)
			{
				this.agent.updateSpeakPermissionNum("");
			}
			isIdle = true;
		}
		else if(title.equals(RtcpMsg.Title_FLOOR_GRANTED))
		{
			agent.goToState(CallConst.FloorState_Permission,null);
		}
	}
	
	public int getState()
	{
		return CallConst.FloorState_NoPermission;
	}
	public void destory()
	{
		agent = null;
	}
//	@Override
//	public String getSpeakName() {
//		// TODO Auto-generated method stub
//		return name;
//	}
}
